<?php

namespace App\HttpController\XAppWxapp\Wxapp\Web;


use App\Common\Log;
use App\Common\WX\Api\MiniProgram;
use App\Common\WX\WxApp;
use App\HttpController\ControllerBase;
use App\Model\t_wx_user;
use XWX\Common\H;

class Api extends ControllerBase
{
    public function index()
    {
        $this->funcWriteCommonR();
    }


    /** 小程序登陆 */
    public function login()
    {
        $code = $this->reqParam('code');
        $iv = $this->reqParam('iv');
        $encryptedData = $this->reqParam('encryptedData');


        $config = $this->funcGetX__config();
        if ($config === null)
        {
            return $this->funcWriteErr(-100, '参数异常');
        }


        $mini_program = MiniProgram::funcIns($config);
        $r = $mini_program->funcGetUserInfo($code);
        if ($r->err())
        {
            return $this->funcWriteErrR($r);
        }


        $openid_app = $r->getData('openid');
        $unionid_app = $r->getData('unionid');
        $session_key = $r->getData('session_key');


        //更新用户资料
        $entity_wxuser = t_wx_user::funcGetWxuser($openid_app, false);
        if (is_null($entity_wxuser))
        {
            $entity_wxuser = new t_wx_user();

            $entity_wxuser->wxuser_openid = $openid_app;
            $entity_wxuser->funcSetX($this->funcGetX());
        }

        //unionid
        if (H::funcStrHasAnyText($unionid_app))
        {
            $entity_wxuser->wxuser_unionid = $unionid_app;
        }

        if (H::funcStrHasAnyText($encryptedData))
        {
            //拿用户详细的授权
            //
            $userinfo_arr = $mini_program->funcDecryptData($session_key, $iv, $encryptedData);
            Log::funcLogDebug(__METHOD__, $userinfo_arr);

            if ($entity_wxuser->wxuser_nickName != $userinfo_arr['nickName']
                || $entity_wxuser->wxuser_headimgurl != $userinfo_arr['avatarUrl']
                || $entity_wxuser->wxuser_sex != $userinfo_arr['gender']
                || $entity_wxuser->wxuser_city != $userinfo_arr['city'])
            {
                $entity_wxuser->wxuser_nickname = $userinfo_arr['nickName'];
                $entity_wxuser->wxuser_sex = $userinfo_arr['gender'];
                $entity_wxuser->wxuser_language = $userinfo_arr['language'];
                $entity_wxuser->wxuser_city = $userinfo_arr['city'];
                $entity_wxuser->wxuser_province = $userinfo_arr['province'];
                $entity_wxuser->wxuser_country = $userinfo_arr['country'];
                $entity_wxuser->wxuser_headimgurl = $userinfo_arr['avatarUrl'];
            }
        }

        if (H::funcStrHasAnyText($entity_wxuser->wxuser_id))
        {
            $entity_wxuser->funcUpdate();
        }
        else
        {
            $entity_wxuser->funcInsert();
        }


        $wxapp = WxApp::funcIns($this->funcGetX__config());

        $timestamp = H::now()->timestamp;
        $token = $wxapp->funcGetSign($entity_wxuser->wxuser_id, $timestamp);


        $this->commonR()->setData('wxuser_id', $entity_wxuser->wxuser_id);
        $this->commonR()->setData('wxuser_headimgurl', $entity_wxuser->wxuser_headimgurl);
        $this->commonR()->setData('wxuser_nickName', $entity_wxuser->wxuser_nickname);
        $this->commonR()->setData('x', $entity_wxuser->funcGetX());
        $this->commonR()->setData('t', $timestamp);
        $this->commonR()->setData('token', $token);
        $this->commonR()->setData('session_key', $session_key);


        $this->funcWriteCommonR();
    }


    /** 手机号 */
    public function getPhone()
    {
        $iv = $this->reqParam('iv');
        $session_key = $this->reqParam('session_key');
        $encryptedData = $this->reqParam('encryptedData');


        $config = $this->funcGetX__config();
        if ($config === null)
        {
            return $this->funcWriteErr(-100, '参数异常');
        }


        $mini_program = MiniProgram::funcIns($config);
        $data = $mini_program->funcDecryptData($session_key, $iv, $encryptedData);

        $this->commonR()->setData('data', $data);

        $this->funcWriteCommonR();
    }
}